package jzqb.service.impl;

import haha.life.common.CutPageBean;
import haha.life.common.SysTool;
import jzqb.bo.DynamicNewsBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.IDynamicNewsDao;
import jzqb.po.AdminUserPo;
import jzqb.po.DynamicNewsPo;
import jzqb.service.IDynamicNewsService;
import jzqb.vo.DynamicNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 14:26
 * @Version 2.0
 */
@Service
public class DynamicNewsServiceImpl implements IDynamicNewsService {
    @Autowired
    private IDynamicNewsDao dynamicNewsDao;
    @Autowired
    private AdminUserInfoDao userInfoDao;

    @Override
    public CutPageBean<DynamicNewsVo> queryDynamicNews(DynamicNewsBo dynamicNewsBo) {
        CutPageBean<DynamicNewsVo> cutPage = new CutPageBean<DynamicNewsVo>();
        List<DynamicNewsVo> dynamicNewsList = dynamicNewsDao.queryDynamicNews_List(dynamicNewsBo);
        if (dynamicNewsList != null && !dynamicNewsList.isEmpty()) { //通知公告集合
            for (DynamicNewsVo dynamicNew : dynamicNewsList) {
                String[] split = dynamicNew.getEntryPerson().split("_");
                if (split.length == 2) { //111_张三  格式存入录入人
                    dynamicNew.setEntryPersonName(split[1]);
                }
            }
        }
        int count = (int) dynamicNewsDao.queryDynamicNews_count(dynamicNewsBo);
        cutPage.initCutPage(count, dynamicNewsBo.getPageSize(), dynamicNewsList);
        return cutPage;
    }

    @Override
    public Boolean addDynamicNews(DynamicNewsPo dynamicNewsPo) throws UnsupportedEncodingException {
        //录入人存 id_name
        String getEntryPersonId = dynamicNewsPo.getEntryPerson();
        if (!"".equals(getEntryPersonId) && getEntryPersonId != null) {
            AdminUserPo user = userInfoDao.findUserById(getEntryPersonId);
            String IdAndName = getEntryPersonId + "_" + user.getName();
            dynamicNewsPo.setEntryPerson(IdAndName);
        }
        return dynamicNewsDao.insertDynamicNews(dynamicNewsPo);
    }

    @Override
    public Boolean updateDynamicNews(DynamicNewsPo dynamicNewsPo) throws UnsupportedEncodingException {
        return dynamicNewsDao.updateDynamicNews(dynamicNewsPo);
    }

    @Override
    public Boolean delDynamicNews(Integer id) {
        return dynamicNewsDao.delDynamicNews(id);
    }

    @Override
    public List<DynamicNewsVo> showDynamicNewsLimitFive() {
        SimpleDateFormat df = new SimpleDateFormat(SysTool.dateFormat_date);
        List<DynamicNewsVo> dynamicNewsList = dynamicNewsDao.showDynamicNewsLimitFive();
        //将 2020-10-10 11:22:33  转换为 2020-10-10
        for (DynamicNewsVo dynamicNew : dynamicNewsList) {
            if ("".equals(dynamicNew.getUpdateTime()) || dynamicNew.getUpdateTime() == null)
                continue;
            try {
                Date date = df.parse(dynamicNew.getUpdateTime());
                String updateTime = df.format(date);
                dynamicNew.setUpdateTime(updateTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dynamicNewsList;
    }

    @Override
    public CutPageBean<DynamicNewsVo> queryDynamicNewsOfFront(DynamicNewsBo dynamicNewsBo) {
        CutPageBean<DynamicNewsVo> cutPage = new CutPageBean<>();
        List<DynamicNewsVo> dynamicNewsList = dynamicNewsDao.queryDynamicNewsOfFrontList(dynamicNewsBo);
        if (dynamicNewsList != null && !dynamicNewsList.isEmpty()) { //通知公告集合
            for (DynamicNewsVo dynamicNew : dynamicNewsList) {
                String[] split = dynamicNew.getEntryPerson().split("_");
                if (split.length == 2) { //111_张三  格式存入录入人
                    dynamicNew.setEntryPersonName(split[1]);
                }
            }
        }
        int count = (int) dynamicNewsDao.queryDynamicNewsOfFrontCount(dynamicNewsBo);
        cutPage.initCutPage(count, dynamicNewsBo.getPageSize(), dynamicNewsList);
        return cutPage;
    }

    @Override
    public DynamicNewsVo getDynamicNewsById(Integer dynamicNewsId) {
        DynamicNewsVo dynamicNews = dynamicNewsDao.getDynamicNewsById(dynamicNewsId);
        if (dynamicNews != null) {
            String entryPerson = dynamicNews.getEntryPerson();
            String[] split = entryPerson.split("_");
            if (split.length == 2) {
                dynamicNews.setEntryPersonName(split[1]);
            }
        }
        return dynamicNews;
    }
}
